﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using MonoTouch.SQLite;
using LinkNote.Core;
using Newtonsoft.Json;

namespace LinkNote.Model
{
    public interface ITree<T>
    {
        int ChildCount { get; }
        List<T> Childs { get; }
    }

    [Buffer]
    public class NoteClass : BaseModel, ITree<NoteClass>
    {
        [PrimaryKey, AutoIncrement]
        public override int ID {get;set;}
        [JsonProperty("id")]
        public int UserID { get; set; }

        public string Name { get; set; }
        [JsonProperty("parentNode")]
        public int ParentID { get; set; }

        public int notecount { get; set; }
        public DateTime createtime { get; set; }
        //public int id;//userId
        public DateTime updatetime { get; set; }
        //public int parentNode;
        public int isDefault { get; set; }
        //public string name;
        public int catalogueid { get; set; }

        public int NoteFileCount
        {
            get
            {
                return NoteFiles.Count;
            }
        }

        private List<LinkNoteFile> noteFiles;
        public List<LinkNoteFile> NoteFiles
        {
            get
            {
                //if (noteFiles == null)
                {
                    if (Name == "回收站")
                        noteFiles = LinkNote.DBHelper.sqlitedb.Table<LinkNoteFile>().Where<LinkNoteFile>(f => f.UserID == this.UserID && f.status==-1).ToList<LinkNoteFile>();
                    else if (Name == "默认笔记本")
                        noteFiles = LinkNote.DBHelper.sqlitedb.Table<LinkNoteFile>().Where<LinkNoteFile>(f => f.UserID == this.UserID && f.status != -1 && (f.ClassID <= 0 || f.ClassID == this.catalogueid)).ToList<LinkNoteFile>();
                    else
                        noteFiles = LinkNote.DBHelper.sqlitedb.Table<LinkNoteFile>().Where<LinkNoteFile>(f => f.ClassID == this.catalogueid && f.status != -1).ToList<LinkNoteFile>();
                }
                return noteFiles;
            }
        }

        public int ChildCount
        {
            get { return Childs.Count; }
        }

        private List<NoteClass> childs;
        public List<NoteClass> Childs
        {
            get
            {
                //if (childs == null)
                {
                    childs = LinkNote.DBHelper.sqlitedb.Table<NoteClass>().
                        Where<NoteClass>(n => n.ParentID == this.catalogueid).ToList<NoteClass>();
                }
                return childs;
            }
        }
    }
}